<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="148px">
        <template slot-scope="scope">
          <span>{{ scope.row.userid }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称" width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号" width="110px">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="QQ" width="110px">
        <template slot-scope="scope">
          <span>{{ scope.row.qq }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="WeChat" width="110px">
        <template slot-scope="scope">
          <span>{{ scope.row.wechat }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="禁言状态" width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.isforbidden == 0 ? '否' : '是' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后操作时间" width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.lastdate | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.isforbidden==1"
            icon="el-icon-edit"
            size="small"
            type="primary"
            @click="forbiddenMember(scope.row.userid,0)"
          >
            解禁
          </el-button>
          <el-button
            v-show="scope.row.isforbidden==0"
            icon="el-icon-edit"
            size="small"
            type="primary"
            @click="forbiddenMember(scope.row.userid,1)"
          >
            禁言
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="listQuery.limit"
      :page.sync="listQuery.page"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchMemberList, updateMemberForbidden } from '@/api/member'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { formatDate } from '../../utils/date'

export default {
  name: 'ArticleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    formatDate(time) {
      time = time * 1000
      const date = new Date(time)
      console.log(new Date(time))
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchMemberList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page_count
        this.listLoading = false
      })
    },
    forbiddenMember(id, state) {
      this.listLoading = true
      var delParam = {}
      delParam.userid = id
      delParam.isforbidden = state
      this.$confirm('将禁言/解禁该用户, 是否确定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateMemberForbidden(delParam).then(response => {
          this.listLoading = false
          this.getList()
        })
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
